/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SystemPanel.java
 *
 * Created on Sep 26, 2011, 6:12:03 PM
 */
package aor2.gui.cpu;

import aor2.cpu.blockinterface.SynchronizationBlock;
import aor2.systembus.SystemBus;
import java.awt.Graphics;

/**
 * 
 * @author Yogi
 */
public class SystemPanel extends javax.swing.JPanel {
	private static SystemPanel singleton = null;

	public static SystemPanel getInstance() {
		if (singleton == null)
			singleton = new SystemPanel();
		return singleton;
	}

	private JLabelSet notRD, notWR, notBUSY, notFC;

	private void makeSignals() {
		notRD = new JLabelSet(false, notRDBUS1, notRDBUS2);
		notWR = new JLabelSet(false, notWRBUS1, notWRBUS2);
		notBUSY = new JLabelSet(false, notBUSYBUS1, notBUSYBUS2);
		notFC = new JLabelSet(false, notFCBUS1, notFCBUS2);

	}

	/** Creates new form SystemPanel */
	private SystemPanel() {
		initComponents();
		makeSignals();
	}

        private void rdZ(){
           notRDBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
           notRDBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
         
           notRDBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
           notRDBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
        }
        
        private void wrZ(){
           notWRBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
           notWRBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
         
           notWRBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
           notWRBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
        }
        
        private void fcZ(){
           notFCBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
           notFCBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_z.jpg"))); // NOI18N
         
           notFCBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
           notFCBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_z.jpg"))); // NOI18N
        }
        
        private void rd1(){
           notRDBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_1.jpg")));
           notRDBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_0.jpg")));
         
           notRDBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_1.jpg")));
           notRDBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_0.jpg")));
        }
        
        private void wr1(){
           notWRBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_1.jpg")));
           notWRBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_0.jpg")));
         
           notWRBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_1.jpg")));
           notWRBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_0.jpg")));
        }
        private void fc1(){
           notFCBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_1.jpg")));
           notFCBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/horiz_1pt_0.jpg")));
         
           notFCBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_1.jpg")));
           notFCBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource(
                   "/aor2/common/resources/vert_1pt_0.jpg")));
        }
        
//	@Override
//	protected void printComponent(Graphics g) {
//		ABUSvalue.setText(Long.toHexString(
//				SystemBus.getInstance().getAddressbusState() & 0xFFFF)
//				.toUpperCase());
//		DBUSvalue.setText(Long.toHexString(
//				SystemBus.getInstance().getDatabusState() & 0xFFFF)
//				.toUpperCase());
//                
//		notBUSY.setEnabled(!SystemBus.getInstance().getBusybus());
//		notWR.setEnabled(!SystemBus.getInstance().getWrbus());
//		notFC.setEnabled(!SystemBus.getInstance().getFcbus());
//		super.printComponent(g);
//	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLayeredPane1 = new javax.swing.JLayeredPane();
        notFCBUS1 = new javax.swing.JLabel();
        notWRBUS1 = new javax.swing.JLabel();
        notRDBUS1 = new javax.swing.JLabel();
        notBUSYBUS1 = new javax.swing.JLabel();
        notBUSYBUS2 = new javax.swing.JLabel();
        notFCBUS2 = new javax.swing.JLabel();
        notWRBUS2 = new javax.swing.JLabel();
        notRDBUS2 = new javax.swing.JLabel();
        registri = new javax.swing.JLabel();
        interfejs = new javax.swing.JLabel();
        operation = new javax.swing.JLabel();
        interr = new javax.swing.JLabel();
        ABUSvalue = new javax.swing.JLabel();
        DBUSvalue = new javax.swing.JLabel();
        M1value = new javax.swing.JLabel();
        M2value = new javax.swing.JLabel();
        notINTR1 = new javax.swing.JLabel();
        notINTR2 = new javax.swing.JLabel();
        notINTR3 = new javax.swing.JLabel();
        inm = new javax.swing.JLabel();
        ctrlUnit = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.GridBagLayout());

        notFCBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notFCBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notFCBUS1.setEnabled(false);
        notFCBUS1.setBounds(0, 136, 330, 20);
        jLayeredPane1.add(notFCBUS1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notWRBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notWRBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notWRBUS1.setEnabled(false);
        notWRBUS1.setBounds(0, 112, 330, 20);
        jLayeredPane1.add(notWRBUS1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notRDBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notRDBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notRDBUS1.setEnabled(false);
        notRDBUS1.setBounds(0, 88, 330, 20);
        jLayeredPane1.add(notRDBUS1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notBUSYBUS1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notBUSYBUS1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notBUSYBUS1.setEnabled(false);
        notBUSYBUS1.setBounds(0, 172, 330, 20);
        jLayeredPane1.add(notBUSYBUS1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notBUSYBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
        notBUSYBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
        notBUSYBUS2.setEnabled(false);
        notBUSYBUS2.setBounds(260, 183, 20, 62);
        jLayeredPane1.add(notBUSYBUS2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notFCBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
        notFCBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
        notFCBUS2.setEnabled(false);
        notFCBUS2.setMaximumSize(new java.awt.Dimension(2, 716));
        notFCBUS2.setMinimumSize(new java.awt.Dimension(2, 716));
        notFCBUS2.setPreferredSize(new java.awt.Dimension(2, 716));
        notFCBUS2.setBounds(230, 147, 100, 98);
        jLayeredPane1.add(notFCBUS2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notWRBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
        notWRBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
        notWRBUS2.setEnabled(false);
        notWRBUS2.setBounds(190, 123, 50, 122);
        jLayeredPane1.add(notWRBUS2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notRDBUS2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_1.jpg"))); // NOI18N
        notRDBUS2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/vert_1pt_0.jpg"))); // NOI18N
        notRDBUS2.setEnabled(false);
        notRDBUS2.setBounds(160, 99, 40, 146);
        jLayeredPane1.add(notRDBUS2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        registri.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        registri.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                registriMouseClicked(evt);
            }
        });
        registri.setBounds(34, 283, 64, 78);
        jLayeredPane1.add(registri, javax.swing.JLayeredPane.DEFAULT_LAYER);

        interfejs.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        interfejs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                interfejsMouseClicked(evt);
            }
        });
        interfejs.setBounds(229, 283, 64, 78);
        jLayeredPane1.add(interfejs, javax.swing.JLayeredPane.DEFAULT_LAYER);

        operation.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        operation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                operationMouseClicked(evt);
            }
        });
        operation.setBounds(34, 400, 64, 78);
        jLayeredPane1.add(operation, javax.swing.JLayeredPane.DEFAULT_LAYER);

        interr.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        interr.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                interrMouseClicked(evt);
            }
        });
        interr.setBounds(229, 400, 64, 78);
        jLayeredPane1.add(interr, javax.swing.JLayeredPane.DEFAULT_LAYER);
        ABUSvalue.setBounds(160, 0, 80, 20);
        jLayeredPane1.add(ABUSvalue, javax.swing.JLayeredPane.DEFAULT_LAYER);
        DBUSvalue.setBounds(150, 30, 80, 20);
        jLayeredPane1.add(DBUSvalue, javax.swing.JLayeredPane.DEFAULT_LAYER);
        M1value.setBounds(120, 280, 0, 0);
        jLayeredPane1.add(M1value, javax.swing.JLayeredPane.DEFAULT_LAYER);
        M2value.setBounds(180, 280, 0, 0);
        jLayeredPane1.add(M2value, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notINTR1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notINTR1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notINTR1.setEnabled(false);
        notINTR1.setBounds(324, 396, 50, 10);
        jLayeredPane1.add(notINTR1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notINTR2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notINTR2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notINTR2.setEnabled(false);
        notINTR2.setBounds(324, 423, 50, 2);
        jLayeredPane1.add(notINTR2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        notINTR3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        notINTR3.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        notINTR3.setEnabled(false);
        notINTR3.setBounds(324, 446, 50, 2);
        jLayeredPane1.add(notINTR3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        inm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_1.jpg"))); // NOI18N
        inm.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/horiz_1pt_0.jpg"))); // NOI18N
        inm.setEnabled(false);
        inm.setBounds(324, 496, 50, 2);
        jLayeredPane1.add(inm, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ctrlUnit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        ctrlUnit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ctrlUnitMouseClicked(evt);
            }
        });
        ctrlUnit.setBounds(14, 564, 299, 37);
        jLayeredPane1.add(ctrlUnit, javax.swing.JLayeredPane.DEFAULT_LAYER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jLayeredPane1, gridBagConstraints);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aor2/common/resources/SystemPanel1.jpg"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 15;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jLabel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

	private void registriMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_registriMouseClicked
		CpuFrame cpu = CpuFrame.getInstance();
		cpu.setCentralPane(RegistriMain.getInstance());
		cpu.repaint();
	}// GEN-LAST:event_registriMouseClicked

	private void interfejsMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_interfejsMouseClicked
		CpuFrame cpu = CpuFrame.getInstance();
		cpu.setCentralPane(InterfaceMain.getInstance());
		cpu.repaint();
	}// GEN-LAST:event_interfejsMouseClicked

	private void operationMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_operationMouseClicked
		CpuFrame cpu = CpuFrame.getInstance();
		cpu.setCentralPane(OperAlu.getInstance());
		cpu.repaint();
	}// GEN-LAST:event_operationMouseClicked

	private void interrMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_interrMouseClicked
		CpuFrame cpu = CpuFrame.getInstance();
		cpu.setCentralPane(intrrMain.getInstance());
		cpu.repaint();
	}

	@Override
	protected void paintComponent(Graphics g) {
		DBUSvalue.setText(Long.toHexString(
				SystemBus.getInstance().getDatabusState() & 0xFFFF)
				.toUpperCase());
		if (SystemBus.getInstance().getAdressbusHigh()) {
			ABUSvalue.setText("Z");
		} else
			ABUSvalue.setText(Long.toHexString(
					SystemBus.getInstance().getAddressbusState() & 0xFFFF)
					.toUpperCase());
		if (SystemBus.getInstance().getDatabusHigh()) {
			DBUSvalue.setText("Z");
		} else
			DBUSvalue.setText(Long.toHexString(
					SystemBus.getInstance().getDatabusState() & 0xFFFF)
					.toUpperCase());

		notBUSY.setEnabled(!SystemBus.getInstance().getBusybus());
		
                if(SystemBus.getInstance().getWrbusHigh()) wrZ();
                else {
                    wr1();
                    notWR.setEnabled(!SystemBus.getInstance().getWrbus());
                }
		if(SystemBus.getInstance().getRdbusHigh()) rdZ();
                else {
                    rd1();
                    notRD.setEnabled(!SystemBus.getInstance().getRdbus());
                }
                if(SynchronizationBlock.getInstance().getfcHighLocal()) fcZ();
                else {
                    fc1();
                    notFC.setEnabled(!SynchronizationBlock.getInstance().getfcLocal());
                }
		super.paintComponent(g);
	}// GEN-LAST:event_interrMouseClicked

	private void ctrlUnitMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_ctrlUnitMouseClicked
		CpuFrame cpu = CpuFrame.getInstance();
		cpu.setCentralPane(Hor2T.getInstance());
		cpu.repaint();
	}// GEN-LAST:event_ctrlUnitMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ABUSvalue;
    private javax.swing.JLabel DBUSvalue;
    private javax.swing.JLabel M1value;
    private javax.swing.JLabel M2value;
    private javax.swing.JLabel ctrlUnit;
    private javax.swing.JLabel inm;
    private javax.swing.JLabel interfejs;
    private javax.swing.JLabel interr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel notBUSYBUS1;
    private javax.swing.JLabel notBUSYBUS2;
    private javax.swing.JLabel notFCBUS1;
    private javax.swing.JLabel notFCBUS2;
    private javax.swing.JLabel notINTR1;
    private javax.swing.JLabel notINTR2;
    private javax.swing.JLabel notINTR3;
    private javax.swing.JLabel notRDBUS1;
    private javax.swing.JLabel notRDBUS2;
    private javax.swing.JLabel notWRBUS1;
    private javax.swing.JLabel notWRBUS2;
    private javax.swing.JLabel operation;
    private javax.swing.JLabel registri;
    // End of variables declaration//GEN-END:variables
}
